package cn.tedu.controller;

import cn.tedu.pojo.Dept;
import cn.tedu.service.DeptService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/sysadmin/dept/")
public class DeptController {
    @Autowired
    private DeptService deptService;
    //查询所有的部门信息
    @RequestMapping("list")
    public String list(Model model) {
        List<Dept> depts = deptService.findAll();
        model.addAttribute("deptList", depts);
        return "/sysadmin/dept/jDeptList";
    }
    //点击禁用实现通用功能
    @RequestMapping("stop")
    public String stop(Model model, @RequestParam(value = "deptId", required = false) String[] deptIds) {
        if (deptIds != null) {
            for (String deptId : deptIds
                    ) {
                deptService.changeState(0, deptId);
            }
        }
        //重定向当前页面
        return "redirect:/sysadmin/dept/list";
    }
    //点击启用实现通用功能
    @RequestMapping("start")
    public String start(Model model, @RequestParam(value = "deptId", required = false) String[] deptIds) {
        if (deptIds != null) {
            for (String deptId : deptIds
                    ) {
                deptService.changeState(1, deptId);
            }
        }
        //重定向当前页面
        return "redirect:/sysadmin/dept/list";
    }

    //批量删除实现通用功能
    @RequestMapping("delete")
    public String delete(Model model, @RequestParam(value = "deptId", required = false) String[] deptIds) {
        if (deptIds != null) {
            //批量删除
            deptService.deleteDeptByIds(deptIds);
        }
        //重定向当前页面
        return "redirect:/sysadmin/dept/list";
    }
    //跳转到新增页面
    @RequestMapping("tocreate")
    public String create(Model model) {
        List<Dept> depts = deptService.findAll();
        model.addAttribute("depts", depts);
        return "/sysadmin/dept/jDeptCreate";
    }
    //保存新增部门的信息的逻辑实现
    @RequestMapping("save")
    public String create(Model model, Dept dept) {
        deptService.saveDept(dept);
        return "redirect:/sysadmin/dept/list";
    }
    //跳转到更新页面，将该部门的信息展示在页面当中
    @RequestMapping("toupdate")
    public String update(Model model, String deptId) {
        //根据id查询该部门的信息
        Dept dept = deptService.findOneById(deptId);
        model.addAttribute("dept",dept);
        //得到所有的部门信息，用于下拉列表
        List<Dept> depts = deptService.findAll();
        model.addAttribute("depts", depts);
        return "sysadmin/dept/jDeptUpdate";
    }
    //更新部门的逻辑实现
    @RequestMapping("update")
    public String updateDept(Model model, Dept dept) {
        deptService.updateDept(dept);
        return "redirect:/sysadmin/dept/list";
    }
    //查看逻辑实现
    @RequestMapping("toview")
    public String toView(Model model,String deptId) {
        Dept dept = deptService.findOneById(deptId);
        model.addAttribute("dept",dept);
        return "/sysadmin/dept/jDeptView";
    }
}
